package com.zhanbo.zhanbowymusic.main;

import android.os.Bundle;

import androidx.annotation.Nullable;

import android.view.Gravity;
import android.view.View;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.utils.MLog;
import com.zhanbo.zhanbowymusic.MainActivity;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentHomeBinding;
import com.zhanbo.zhanbowymusic.dialog.BuyVipDialog;
import com.zhanbo.zhanbowymusic.dialog.PlayListDialog;
import com.zhanbo.zhanbowymusic.dialog.VipBuyPop;
import com.zhanbo.zhanbowymusic.main.bean.CreatePlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.FeaturedPlaylistDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.PCZoneListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.PCZoneListEBBEan;
import com.zhanbo.zhanbowymusic.main.bean.RanKingListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.SettingEBBean;
import com.zhanbo.zhanbowymusic.main.bean.SingerAlbumDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.bean.SingerListDetailsEBBean;
import com.zhanbo.zhanbowymusic.main.featured.FeaturedPlayListDetailsFragment;
import com.zhanbo.zhanbowymusic.main.featured.PCZoneFragment;
import com.zhanbo.zhanbowymusic.main.featured.RanKingListDetailsFragment;
import com.zhanbo.zhanbowymusic.main.featured.SingerAlbumDetailsFragment;
import com.zhanbo.zhanbowymusic.main.featured.SingerListDetailsFragment;
import com.zhanbo.zhanbowymusic.main.mine.create.CreatePlaylistDetailsFragment;
import com.zhanbo.zhanbowymusic.play.MusicPlayFragment;
import com.zhanbo.zhanbowymusic.play.MvPlayFragment;
import com.zhanbo.zhanbowymusic.search.SearchFragment;
import com.zhanbo.zhanbowymusic.search.bean.SearchEBBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * 首页
 */
public class HomeFragment extends BaseFragment<FragmentHomeBinding> implements View.OnClickListener {
    private ContentFragment contentFragment;
    private CreatePlaylistDetailsFragment createPlaylistDetailsFragment;
    private FeaturedPlayListDetailsFragment featuredSongListDetailsFragment;
    private RanKingListDetailsFragment ranKingListDetailsFragment;
    private SingerListDetailsFragment singerListDetailsFragment;
    private SingerAlbumDetailsFragment singerAlbumDetailsFragment;
    private PCZoneFragment pcZoneFragment;
    private PCZoneListDetailsFragment pcZoneListDetailsFragment;
    private SearchFragment searchFragment;
    private SettingFragment settingFragment;


    @Override
    protected void init(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        contentFragment = ContentFragment.newInstance();
        if (findChildFragment(ContentFragment.class) == null) {
            loadRootFragment(R.id.fl_home, contentFragment);
        }
//        start(MusicPlayFragment.newInstance(),SINGLETASK);
        initView();
        initListener();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }

    @Override
    protected Object getLayout() {
        MLog.e("R.layout.fragment_home");

        return R.layout.fragment_home;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openCreatePlaylistDetails(CreatePlaylistDetailsEBBean createPlaylistDetailsEBBean){
        if (createPlaylistDetailsFragment==null){
            createPlaylistDetailsFragment  = CreatePlaylistDetailsFragment.newInstance();
        }
        start(createPlaylistDetailsFragment,SINGLETASK);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openSetting(SettingEBBean settingEBBean){
        if (settingFragment==null){
            settingFragment = SettingFragment.newInstance();
        }
        start(settingFragment,SINGLETASK);

    }

    /**
     * 歌单详情
     * @param featuredPlaylistDetailsEBBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openFeaturedPlaylistDetails(FeaturedPlaylistDetailsEBBean featuredPlaylistDetailsEBBean){
//        if (featuredSongListDetailsFragment==null){
            featuredSongListDetailsFragment  = FeaturedPlayListDetailsFragment.newInstance(featuredPlaylistDetailsEBBean.getItemsDTO());
//        }
//        featuredSongListDetailsFragment = new FeaturedPlayListDetailsFragment();
        start(featuredSongListDetailsFragment,SINGLETASK);
    }
    /**
     * 榜单详情
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openFeaturedPlaylistDetails(RanKingListDetailsEBBean ranKingListDetailsEBBean){
//        if (featuredSongListDetailsFragment==null){
        ranKingListDetailsFragment  = ranKingListDetailsFragment.newInstance(ranKingListDetailsEBBean.getItemsDTO());
//        }
//        featuredSongListDetailsFragment = new FeaturedPlayListDetailsFragment();
        start(ranKingListDetailsFragment,SINGLETASK);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openSingerListDetails(SingerListDetailsEBBean singerListDetailsEBBean){
//        if (singerListDetailsFragment==null){
            singerListDetailsFragment  = SingerListDetailsFragment.newInstance(singerListDetailsEBBean.getItemsDTO());
//        }
        start(singerListDetailsFragment,SINGLETASK);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openSingerAlbumDetails(SingerAlbumDetailsEBBean singerAlbumDetailsEBBean){
//        if (singerAlbumDetailsFragment==null){
            singerAlbumDetailsFragment  = SingerAlbumDetailsFragment.newInstance(singerAlbumDetailsEBBean.getItemsDTO());
//        }
        start(singerAlbumDetailsFragment,SINGLETASK);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openPCZoneList(PCZoneListEBBEan pcZoneListEBBEan){
        if (pcZoneFragment==null){
            pcZoneFragment  = PCZoneFragment.newInstance();
        }
        start(pcZoneFragment,SINGLETASK);
    }

    /**
     * 亲子专区歌单详情跳转
     * @param pcZoneListDetailsEBBean
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openPCZoneListDetails(PCZoneListDetailsEBBean pcZoneListDetailsEBBean){
        if (pcZoneListDetailsFragment==null){
            pcZoneListDetailsFragment  = PCZoneListDetailsFragment.newInstance();
        }
        start(pcZoneListDetailsFragment,SINGLETASK);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void openSearch(SearchEBBean searchEBBean){
        if (searchFragment==null){
            searchFragment  = SearchFragment.newInstance();
        }
        start(searchFragment,SINGLETASK);
    }

    @Override
    protected void initListener() {
        binding.rlFeatured.setOnClickListener(this);
        binding.rlCollections.setOnClickListener(this);
        binding.rlCreatePlaylist.setOnClickListener(this);
        binding.rlLike.setOnClickListener(this);
        binding.rlRecentlyPlayed.setOnClickListener(this);
        binding.rlPodcast.setOnClickListener(this);
        binding.ivSetting.setOnClickListener(this);
        binding.ivVip.setOnClickListener(this);
        binding.btnSearch.setOnClickListener(this);

    }

    public static HomeFragment newInstance() {

        Bundle args = new Bundle();

        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.rl_featured) { //云音乐精选
            contentFragment.setPage(0);
            clearBg();
            binding.rlFeatured.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivFeaturedIcon.setImageResource(R.mipmap.jingxuanicon);
            binding.tvFeatured.setTextColor(getResources().getColor(R.color.white));

        } else if (v.getId() == R.id.rl_podcast) { //播客
            contentFragment.setPage(1);
            clearBg();
            binding.rlPodcast.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivPodcastIcon.setImageResource(R.mipmap.bokeicon);
            binding.tvPodcast.setTextColor(getResources().getColor(R.color.white));


        } else if (v.getId() == R.id.rl_like) { //我喜欢的音乐
            contentFragment.setPage(2);
            clearBg();
            binding.rlLike.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivLikeIcon.setImageResource(R.mipmap.woxihuanicon);
            binding.tvLike.setTextColor(getResources().getColor(R.color.white));


        } else if (v.getId() == R.id.rl_recently_played) { //最近播放
            contentFragment.setPage(3);
            clearBg();
            binding.rlRecentlyPlayed.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivRecentlyPlayedIcon.setImageResource(R.mipmap.zuijinicon);
            binding.tvRecentlyPlayed.setTextColor(getResources().getColor(R.color.white));


        } else if (v.getId() == R.id.rl_collections) { //我的收藏
            contentFragment.setPage(4);
            clearBg();
            binding.rlCollections.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivCollectionsIcon.setImageResource(R.mipmap.shoucangicon);
            binding.tvCollections.setTextColor(getResources().getColor(R.color.white));


        } else if (v.getId() == R.id.rl_create_playlist) { //创建的歌单
            contentFragment.setPage(5);
            clearBg();
            binding.rlCreatePlaylist.setBackground(getResources().getDrawable(R.drawable.tv_home_type_bg));
            binding.ivCreatePlaylistIcon.setImageResource(R.mipmap.gedanicon);
            binding.tvCreatePlaylist.setTextColor(getResources().getColor(R.color.white));

        } else if (v.getId() == R.id.iv_setting) { //设置页面
            EventBus.getDefault().post(new SettingEBBean());

        } else if (v.getId() == R.id.iv_vip) { //VIP弹窗
            MainActivity mainActivity  = (MainActivity) getActivity();
            mainActivity.showBuyVipDialog();

        }else if(v.getId() == R.id.btn_search){//搜索页
            EventBus.getDefault().post(new SearchEBBean());
        }

    }

    private void clearBg() {
        binding.rlFeatured.setBackground(null);
        binding.ivFeaturedIcon.setImageResource(R.mipmap.jingxuanicono);
        binding.tvFeatured.setTextColor(getResources().getColor(R.color.color_text));
        binding.rlPodcast.setBackground(null);
        binding.ivPodcastIcon.setImageResource(R.mipmap.bokeiconno);
        binding.tvPodcast.setTextColor(getResources().getColor(R.color.color_text));
        binding.rlLike.setBackground(null);
        binding.ivLikeIcon.setImageResource(R.mipmap.woxihuaniconno);
        binding.tvLike.setTextColor(getResources().getColor(R.color.color_text));
        binding.rlRecentlyPlayed.setBackground(null);
        binding.ivRecentlyPlayedIcon.setImageResource(R.mipmap.zuijiniconno);
        binding.tvRecentlyPlayed.setTextColor(getResources().getColor(R.color.color_text));
        binding.rlCollections.setBackground(null);
        binding.ivCollectionsIcon.setImageResource(R.mipmap.shoucangiconno);
        binding.tvCollections.setTextColor(getResources().getColor(R.color.color_text));
        binding.rlCreatePlaylist.setBackground(null);
        binding.ivCreatePlaylistIcon.setImageResource(R.mipmap.gedaniconno);
        binding.tvCreatePlaylist.setTextColor(getResources().getColor(R.color.color_text));

    }

    public void startMusicpage() {

    }
}